package com.zte.claa.inficombo.csa.app.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zte.claa.inficombo.csa.app.controller.ret.ApiResult;
import com.zte.claa.inficombo.csa.app.service.nbiot.NBIotDevDataRptService;

/**
 * 电信天翼平台应用数据推送接收Controller类:
 *  URL:  http://[inficombo-csa-ip]:8098/claa/csa/ctcc/aep/appdatarpt
 * 
 * @Controller
 * @EnableAutoConfiguration
 */
@RestController
@RequestMapping("/claa/csa/ctcc/aep") 
public class CtccAepRptController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(CtccAepRptController.class);
    
    @Autowired
    private NBIotDevDataRptService rptService;
    
    /**
     * 以JSON格式推送至订阅的URL地址.
     * 目前，订阅接收方收到消息后，需固定返回HTTP 200，其它返回码平台会认为推送失败。
     * 消息推送支持失败重传机制，推送失败（超时、HTTP返回码不等于200等）的消息，平台最多会重试3次。
     * 如果消息接收服务连续失败次数超过10次，平台会认为该服务暂时不可用，标记该消息接收服务处于失败冷却状态，
     * 1分钟内不再向该消息接收服务推送消息（这1分钟内的相关消息将不会被推送）.
     */
    @RequestMapping(value = "/appdatarpt", method=RequestMethod.POST) 
    @ResponseBody
    public ApiResult<?> appdatarpt(HttpServletRequest request) throws Exception { 
    	String rptJson = this.getJsonParam(request);
        logger.info("[CTCC-AEP]Receive origin rptdata: " +rptJson);
        this.rptService.submitCtccAepRptData(rptJson);
        ApiResult<String> ret = new ApiResult<String>();
        ret.setResult(ApiResult.RET_SUCC);
        return ret;
    }

}
